package com.meiyu.live.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.meiyu.live.R;


/**
 * Created by zbl on 2016/8/11.
 */
public class HoverImageView extends ImageView {
    private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
    private static final PorterDuffXfermode duffMode = new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP);

    private int borderColor = 0x44000000;
    private Paint borderPaint, paint;
    private Path boundPath = new Path();
    private Path borderPath = new Path();
    private RectF rect = new RectF();
    private float borderWidth = 1f;

    public HoverImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup(attrs);
    }

    public HoverImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup(attrs);
    }

    public HoverImageView(Context context) {
        super(context);
        setup(null);
    }

    private void setup(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RoundImageView);
            borderColor = typedArray.getColor(R.styleable.RoundImageView_borderColor, borderColor);
            borderWidth = typedArray.getDimension(R.styleable.RoundImageView_borderWidth, borderWidth);
            typedArray.recycle();
        }
        borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setStrokeWidth(borderWidth + 1);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setStyle(Style.FILL);

//        super.setScaleType(SCALE_TYPE);
    }

    protected void drawBorder(Canvas canvas) {
        if (borderPath != null && borderPaint != null) {
            borderPaint.setStyle(Style.STROKE);
            borderPaint.setColor(borderColor);
            canvas.drawPath(borderPath, borderPaint);
        }
    }

    public void buildBorderPath(Path borderPath) {
        borderPath.reset();
        final float halfBorderWidth = borderWidth * 0.5f;
        boundPath.addRect(halfBorderWidth, halfBorderWidth,
                getWidth() - halfBorderWidth, getHeight() - halfBorderWidth, Direction.CW);
    }

    public void buildBoundPath(Path boundPath) {
        boundPath.reset();
        boundPath.addRect(0, 0, getWidth(), getHeight(), Direction.CW);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (changed) {
            buildBoundPath(boundPath);
            buildBorderPath(borderPath);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        rect.set(0, 0, getWidth(), getHeight());

        int saveCount = canvas.saveLayer(rect, null,
                Canvas.MATRIX_SAVE_FLAG |
                        Canvas.CLIP_SAVE_FLAG |
                        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                        Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                        Canvas.CLIP_TO_LAYER_SAVE_FLAG);

//        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
        super.onDraw(canvas);
        final int color = 0xffffffff;
        paint.setColor(color);
        paint.setXfermode(duffMode);
        canvas.drawPath(boundPath, paint);
        canvas.restoreToCount(saveCount);

        drawBorder(canvas);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
    }

    public float getBorderWidth() {
        return borderWidth;
    }
}
